import java.io.*;
import java.net.*;
import java.util.BitSet;
import trigest.trigest;

public class TCPClient{

	public static void main(String argv[]) throws Exception
	{
		String output;
		String input;
		String value;
		char ah[];

		BitSet bit,bit1,bitoriginal;
		int n=0;
		byte[] signature=new byte[1024];
		trigest mike,mike2;
		byte[] signature2=new byte[1024];
		Socket clientSocket = new Socket("127.0.0.1", 6606);
		DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());		
		BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));


		output="P|malsrou|";
		outToServer.writeBytes(output + '\n');
		
		input = inFromServer.readLine();
		System.out.println(input);
	
	
		
		
		mike=new trigest("I am a chicken that has a lot to eat but cant find anything of that sort. Really doesnt matter");
		mike2=new trigest("chicken");
		

		signature=mike.getSignature();

        String result = ""; //code for converting the byte arrays into hex string
        for (int i=0; i < signature.length; i++)
        {
            result +=Integer.toString( ( signature[i] & 0xff ) + 0x100, 16).substring( 1 );
        }
        
		

        //BitSet bits3 = new BitSet();
      

        System.out.println();
        for(int i=0;i<8192;i++)
        System.out.println();
		signature2=mike2.getSignature();
        String result2="";
        for (int i=0; i < signature2.length; i++)
        {
            result2 +=Integer.toString( ( signature2[i] & 0xff ) + 0x100, 16).substring(1);
        }
	

        if(result2.contains(result))
        {
        	System.out.println("MATCH!!!");
        }
        else
        {

        	System.out.println("NO MATCH!!!");
        }
		//bit=fromByteArray(signature);
		//bit1=fromByteArray(result.getBytes());
		//bitoriginal=fromByteArray(signature);
		//bit.and(bit1);
		//if(bit.cardinality()==bitoriginal.cardinality())
	//	{
		//	System.out.println("Match");
		//}
		
		System.out.println("Values of the Bitset:");
		System.out.println(result);
		System.out.println(result2);

		
		if(input.startsWith("T"))
		{
			
		for(n=0;n<3;n++)
		{
		output="T|Chiken|I am a lonly chiken that has a lot to eat but cant find anything of that sort. Really doesnt matter|" + signature.toString() + "|malsrou|92.164.32.1|500|18:00 Monday|";
		outToServer.writeBytes(output + '\n');
		System.out.println("I am here " + n);
		input = inFromServer.readLine();
		System.out.println(input);
		System.in.read();
		}
		
		}

		
		
		clientSocket.close();
	
	}

	
private static BitSet fromByteArray(byte[] bytes) {
	    BitSet bits = new BitSet();
	    for (int i=0; i<bytes.length*8; i++) {
	        if ((bytes[bytes.length-i/8-1]&(1<<(i%8))) > 0) {
	            bits.set(i);
	        }
	    }
	    return bits;
}

	
}

